package com.forum.service.impl;

import com.forum.bo.TipBo;
import com.forum.common.pojo.PageResult;
import com.forum.mapper.*;
import com.forum.pojo.Reply;
import com.forum.pojo.Tab;
import com.forum.pojo.Tip;
import com.forum.pojo.User;
import com.forum.service.TipService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.BooleanUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author Mirror
 * @CreateDate 2020/3/22.
 * 帖子业务处理实现
 */
@Service
public class TipServiceImpl implements TipService {

    @Autowired
    private TipMapper tipMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ReplyMapper replyMapper;

    @Autowired
    private TabMapper tabMapper;

    @Autowired
    private ForumMapper forumMapper;
    /**
     * 根据条件查询你帖子
     * @param page
     * @param rows
     * @param tipTitle  标题
     * @param recently  最近
     * @param active    活跃
     * @param essence   精华
     * @return
     */
    @Override
    public PageInfo<TipBo> queryTipsByCond(Integer page, Integer rows, String tipTitle, Boolean recently, Boolean active, Boolean essence) {
        //初始化example对象
        Example example = new Example(Tip.class);
        Example.Criteria criteria = example.createCriteria();
        //根据标题模糊查询
        if (StringUtils.isNotBlank(tipTitle)){
            criteria.andLike("tipTitle","%"+tipTitle+"%");
        }
        //添加查询最近帖子条件
        if (BooleanUtils.isTrue(recently)){
            example.setOrderByClause("tip_publishTime"+" "+"desc");
        }
        //添加查询活跃帖子条件
        if (BooleanUtils.isTrue(active)){

        }
        //添加查询精华帖子条件
        if (BooleanUtils.isTrue(essence)){

        }
        //添加分页条件
        PageHelper.startPage(page,rows);
        //执行查询
        List<Tip> tips = this.tipMapper.selectByExample(example);

        //包装结果数据
        List<TipBo> tipBos = tips.stream().map(tip -> {
            TipBo tipBo = new TipBo();
            // copy共同属性的值到新的对象
            BeanUtils.copyProperties(tip, tipBo);
            //获取用户名字
            tipBo.setUsername(this.userMapper.selectByPrimaryKey(tip.getUserId()).getUserName());
            //获取分类名称
            Tab tab = this.tabMapper.selectByPrimaryKey(tip.getTabId());
            tipBo.setTabName(tab.getTabName());
            tipBo.setForumName(this.forumMapper.selectByPrimaryKey(tab.getForumId()).getForumName());
            return tipBo;
        }).collect(Collectors.toList());
        PageInfo<TipBo> tipBoPageInfo = new PageInfo<>(tipBos);

        return tipBoPageInfo;
    }

    /**
     * 根据帖子ID查询帖子详情
     * @param tipId
     * @return
     */
    @Override
    public TipBo queryTipByTipId(Long tipId) {
        //查询帖子信息
        Tip tip = this.tipMapper.selectByPrimaryKey(tipId);
        //查询帖子回复信息
        Reply reply = new Reply();
        reply.setTipId(tip.getTipId());
        List<Reply> replies = this.replyMapper.select(reply);

        //封装结果数据
        TipBo tipBo = new TipBo();
        BeanUtils.copyProperties(tip,tipBo);
        tipBo.setReply(replies);
        return tipBo;
    }
}
